package com.tzy.myfootprint.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tzy.common.constant.WebResult;
import com.tzy.common.constant.WebResultMsgCode;
import com.tzy.common.controller.BaseController;
import com.tzy.common.entity.MyFootprint;
import com.tzy.myfootprint.service.IMyFootprintService;
import org.apache.ibatis.logging.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
/**
 * <p>
 * 我的足迹表 前端控制器
 * </p>
 *
 * @author ljh
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/footprint")
public class MyFootprintController extends BaseController {
	private Logger log = LoggerFactory.getLogger("MyFootprintLog");
	@Resource
	private IMyFootprintService iMyFootprintService;


	@GetMapping("getMyFootprint")
	@Cacheable(value = "get_my_footprint", unless = "#request == null ")
	public WebResult getMyFootprint(HttpServletRequest request){
		int userId = getUserId(request);
		List<MyFootprint> list = iMyFootprintService.listFootprint(userId);
		return new WebResult(WebResultMsgCode.SUCCESS).putData("list", list);
	}

//	@PostMapping("deleteFootprint")
//	public WebResult deleteFootprint(int shopId){
//		QueryWrapper<MyFootprint> queryWrapper = new QueryWrapper<>();
//		queryWrapper.eq("shop_id", shopId);
//		boolean judge = iMyFootprintService.remove(queryWrapper);
//		return new WebResult(WebResultMsgCode.SUCCESS).putData("judge", judge);
//	}

	@PostMapping("addFootprint")
	public WebResult addFootprint(HttpServletRequest request, @RequestBody MyFootprint footprint){
		log.info("shopId:"+footprint.getShopId());
		int userId = getUserId(request);
		footprint.setUserId(userId);
		boolean judge = false;
//		//判断这个用户里面是否有这个店铺ID
		boolean count = iMyFootprintService.countFootprint(footprint);
		//如果没有这个店铺ID则运行该方法
		if(count){
			//添加成功返回true
			judge = iMyFootprintService.addFootprint(footprint);
		}
		return new WebResult(WebResultMsgCode.SUCCESS).putData("judge", judge);

	}

	@PostMapping("deleteFootprint")
	@CacheEvict(value = "get_my_footprint",allEntries = true)
	public WebResult deleetFootprint(HttpServletRequest request,@RequestBody Integer[] id){
		log.info("id:"+id);
//		int userId = getUserId(request);
//		log.info("userId:"+userId);
//		转换类型
		List<Integer> list= Arrays.asList(id);
//		boolean judge = iMyFootprintService.deleteFootprint(userId, shopId);
		boolean judge = iMyFootprintService.removeByIds(list);
		log.info("judge:"+judge);
		return new WebResult(WebResultMsgCode.SUCCESS).putData("judge", judge);
	}



}
